﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.ZhaoLuService;
using org.in2bits.MyXls;

namespace CommunitySystem
{
    public partial class MianShiControl_ZLGL : UserControl
    {
        private ZhaoLuService.ZhaoLuServiceClient Client1 = new ZhaoLuServiceClient();

        private ObservableCollection<ZhaoLu_DaoRuKaoShengModel> selectedempmodel =
            new ObservableCollection<ZhaoLu_DaoRuKaoShengModel>();
        public MianShiControl_ZLGL()
        {
            InitializeComponent();
            for (int i = DateTime.Today.Year; i > DateTime.Today.Year - 10; i--)
            {
                ComboBoxItem cbi = new ComboBoxItem();
                cbi.Content = i.ToString();
                cbi.Tag = i;
                nian.Items.Add(cbi);
            }
            nian.SelectedIndex = 0;
            Client1.GetZhaoLu_DaoRuKaoShengModelCompleted += new EventHandler<GetZhaoLu_DaoRuKaoShengModelCompletedEventArgs>(Client1_GetZhaoLu_DaoRuKaoShengModelCompleted);
            Client1.NiTongYiorNot_ZLCompleted += new EventHandler<NiTongYiorNot_ZLCompletedEventArgs>(Client1_NiTongYiorNot_ZLCompleted);
            Client1.QiQuanCompleted += new EventHandler<QiQuanCompletedEventArgs>(Client1_QiQuanCompleted);
            Client1.UpdateZL_MianShiFenShuCompleted += new EventHandler<UpdateZL_MianShiFenShuCompletedEventArgs>(Client1_UpdateZL_MianShiFenShuCompleted);
            Load();
        }

        private string pattern = @"^[0-9]+([.]{1}[0-9]+){0,1}$";
        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                double.Parse(((TextBox)sender).Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
                ((TextBox)sender).Text = "";
            }
        }

        private void Text_LostFocus(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox)sender).Text))
            {
                if (Convert.ToDecimal(((TextBox)sender).Text) > 100)
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("输入的成绩有误，请重新输入！");
                    return;
                }
                if (temp.Where(p => p.ID == Convert.ToInt32(((TextBox)sender).Tag)).FirstOrDefault() != null)
                {
                    temp.Where(p => p.ID == Convert.ToInt32(((TextBox)sender).Tag)).FirstOrDefault().MianShiScore = Convert.ToDecimal(((TextBox)sender).Text);
                    temp.Where(p => p.ID == Convert.ToInt32(((TextBox)sender).Tag)).FirstOrDefault().TotalScore =
                        Convert.ToDecimal(Math.Round(
                                (Math.Round(Convert.ToDouble(temp.Where(p => p.ID == Convert.ToInt32(((TextBox)sender).Tag)).FirstOrDefault().ZongFen) * 0.4, 2) +
                                 Math.Round(Convert.ToDouble(((TextBox)sender).Text) * 0.6, 2)), 2).ToString());
                }
                
            }
        }

        public void Load()
        {
            Client1.GetZhaoLu_DaoRuKaoShengModelAsync(DateTime.Today.Year - nian.SelectedIndex, false, yue.SelectedIndex + 1,0);
        }

        private ObservableCollection<ZhaoLu_DaoRuKaoShengModel> temp =
            new ObservableCollection<ZhaoLu_DaoRuKaoShengModel>();
        void Client1_GetZhaoLu_DaoRuKaoShengModelCompleted(object sender, GetZhaoLu_DaoRuKaoShengModelCompletedEventArgs e)
        {
             temp=  (ObservableCollection<ZhaoLu_DaoRuKaoShengModel>)e.Result;
            List<string> tempstring = new List<string>();
            if (temp != null && temp.Count > 0)
            {
                for (int i = 0; i < temp.Count; i++)
                {
                    if (!tempstring.Contains(temp[i].YRBM + "-" + temp[i].BBZW ))
                        tempstring.Add(temp[i].YRBM + "-" + temp[i].BBZW);

                }
                for (int i = 0; i < tempstring.Count; i++)
                {
                    for (int n = 0; n < temp.Where(p => p.YRBM == tempstring[i].Split('-')[0] &&
                                                                        p.BBZW == tempstring[i].Split('-')[1]).Count(); n++)
                    {
                        temp.Where(p => p.YRBM == tempstring[i].Split('-')[0] &&
                                        p.BBZW == tempstring[i].Split('-')[1] ).ToList()[n].MingCi = n + 1;
                    }
                }
            }

            //for (int i = 0; i < temp.Count; i++)
            //{
            //    temp[i].Sec = i + 1;
            //}
            DGRole.ItemsSource = null;
            DGRole.ItemsSource = temp;
        }

        private void Data_Click(object sender, RoutedEventArgs e)
        {
            if(DGRole.ItemsSource != null)
            {
                foreach (ZhaoLu_DaoRuKaoShengModel model in (ObservableCollection<ZhaoLu_DaoRuKaoShengModel>)DGRole.ItemsSource)
                {
                    Client1.UpdateZL_MianShiFenShuAsync(model);
                    count = count + 1;
                }
            }
            //selectedempmodel.Clear();
            //if (DGRole.ItemsSource != null)
            //{
            //    foreach (ZhaoLu_DaoRuKaoShengModel i in (ObservableCollection<ZhaoLu_DaoRuKaoShengModel>)DGRole.ItemsSource)
            //    {
            //        if (i.IsSelected == true && selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() == null)
            //        {
            //            selectedempmodel.Add(i);
            //        }
            //        if (selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
            //        {
            //            selectedempmodel.Remove(selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault());
            //        }
            //    }
            //}
            //if (selectedempmodel.Count > 0)
            //{
            //    if (selectedempmodel.Count > 1)
            //    {
            //        SilverlightMessageBoxLibrary.Message.ErrorMessage("只能选择一个人员进行成绩编辑！");
            //        return;
            //    }
            //    ChengJiWindow_ZL RMWindow = new ChengJiWindow_ZL(Convert.ToInt32(selectedempmodel[0].ID));
            //    RMWindow.Closed += new EventHandler(RMWindow_Closed);
            //    RMWindow.Show();


            //}
            //else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要编辑的人员！");
        }

        private int count = 0;
        void Client1_UpdateZL_MianShiFenShuCompleted(object sender, UpdateZL_MianShiFenShuCompletedEventArgs e)
        {
            if (count == ((ObservableCollection<ZhaoLu_DaoRuKaoShengModel>)DGRole.ItemsSource).Count)
            {
                count = count - 1;
                SilverlightMessageBoxLibrary.Message.InfoMessage("编辑成功!");
                Load();
            }
            
        }
        void RMWindow_Closed(object sender, EventArgs e)
        {
            Load();
        }

        private void nitongyi_Click(object sender, RoutedEventArgs e)
        {
            selectedempmodel.Clear();
            if (DGRole.ItemsSource != null)
            {
                foreach (ZhaoLu_DaoRuKaoShengModel i in (ObservableCollection<ZhaoLu_DaoRuKaoShengModel>)DGRole.ItemsSource)
                {
                    if (i.MianShiScore <= 0)
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage("所选人员还有未输入面试成绩，不可拟同意！");
                        return;
                    }
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault());
                    }
                }
            }
            if (selectedempmodel.Count > 0)
            {
                if (MessageBox.Show("确定要拟同意所选人员吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    ObservableCollection<int> ids = new ObservableCollection<int>();

                    foreach (ZhaoLu_DaoRuKaoShengModel i in selectedempmodel)
                    {
                        ids.Add(i.ID);
                    }
                    Client1.NiTongYiorNot_ZLAsync(ids, "拟同意");
                }
            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要拟同意的人员！");
        }
        void Client1_NiTongYiorNot_ZLCompleted(object sender, NiTongYiorNot_ZLCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("成功!");
                Load();
            }
        }

        private void fangqi_Click(object sender, RoutedEventArgs e)
        {
            if (DGRole.ItemsSource != null)
            {
                foreach (ZhaoLu_DaoRuKaoShengModel i in (ObservableCollection<ZhaoLu_DaoRuKaoShengModel>)DGRole.ItemsSource)
                {
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault());
                    }
                }
            }
            if (selectedempmodel.Count > 0)
            {
                if (MessageBox.Show("确定进行弃权操作吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    string ss = "";
                    foreach (ZhaoLu_DaoRuKaoShengModel i in selectedempmodel)
                    {
                        ss += i.ID.ToString() + ",";
                    }
                    Client1.QiQuanAsync(ss.Substring(0, ss.Length - 1), "面试后");
                }

            }
        }
        void Client1_QiQuanCompleted(object sender, QiQuanCompletedEventArgs e)
        {
            MessageBox.Show("弃权成功！");
            Load();
        }
        private Stream ss = null;
        private void SaveData_Click(object sender, RoutedEventArgs e)
        {
            if (DGRole.ItemsSource != null)
            {
                foreach (ZhaoLu_DaoRuKaoShengModel i in (ObservableCollection<ZhaoLu_DaoRuKaoShengModel>)DGRole.ItemsSource)
                {
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault());
                    }
                }
            }
            if (selectedempmodel.Count > 0)
            {
                var downLoad = new SaveFileDialog()
                {
                    //Filter = "Excel files(2007~2010) (*.xlsx)|*.xlsx|Excel files(2003) (*.xls)|*.xls", //"Excel files (*.xlsx)|*.xlsx|(*.xls)|*.xls",
                    Filter = "Excel files(2003) (*.xls)|*.xls",
                };
                if (downLoad.ShowDialog() == true)
                {
                    ss = downLoad.OpenFile();
                    PageInit();
                }
            }
        }
        public void PageInit()
        {
            XlsDocument xls = new XlsDocument();
            XF xf = xls.NewXF();
            xf.HorizontalAlignment = HorizontalAlignments.Centered;
            xf.VerticalAlignment = VerticalAlignments.Centered;
            xf.UseBorder = true;
            xf.TopLineStyle = 1;
            xf.TopLineColor = org.in2bits.MyXls.Colors.Black;
            xf.BottomLineStyle = 1;
            xf.BottomLineColor = org.in2bits.MyXls.Colors.Black;
            xf.LeftLineStyle = 1;
            xf.LeftLineColor = org.in2bits.MyXls.Colors.Black;
            xf.RightLineStyle = 1;
            xf.RightLineColor = org.in2bits.MyXls.Colors.Black;
            xf.TextWrapRight = true;

            XF xf1 = xls.NewXF();
            xf1.UseBorder = true;
            xf1.TopLineStyle = 1;
            xf1.TopLineColor = org.in2bits.MyXls.Colors.Black;
            xf1.BottomLineStyle = 1;
            xf1.BottomLineColor = org.in2bits.MyXls.Colors.Black;
            xf1.LeftLineStyle = 1;
            xf1.LeftLineColor = org.in2bits.MyXls.Colors.Black;
            xf1.RightLineStyle = 1;
            xf1.RightLineColor = org.in2bits.MyXls.Colors.Black;
            xf1.HorizontalAlignment = HorizontalAlignments.Centered;
            xf1.VerticalAlignment = VerticalAlignments.Centered;
            xf1.Font.Bold = true;
            xf1.Font.Height = 15 * 20;

            XF xf3 = xls.NewXF();
            xf3.UseBorder = true;
            xf3.TopLineStyle = 1;
            xf3.TopLineColor = org.in2bits.MyXls.Colors.Black;
            xf3.BottomLineStyle = 1;
            xf3.BottomLineColor = org.in2bits.MyXls.Colors.Black;
            xf3.LeftLineStyle = 1;
            xf3.LeftLineColor = org.in2bits.MyXls.Colors.Black;
            xf3.RightLineStyle = 1;
            xf3.RightLineColor = org.in2bits.MyXls.Colors.Black;
            xf3.HorizontalAlignment = HorizontalAlignments.Centered;
            xf3.VerticalAlignment = VerticalAlignments.Centered;
            xf3.Font.Bold = true;
            xf3.Font.Height = 12 * 20;

            Worksheet sheet1 = xls.Workbook.Worksheets.AddNamed("人员面试表");
            sheet1.Cells.Merge(1, 1, 1, 9);
            sheet1.Cells.Add(1, 1, "徐汇区" + (DateTime.Today.Year - nian.SelectedIndex).ToString() + "年招录人员面试表", xf1);
            sheet1.Cells.Add(2, 1, "序号", xf3);
            sheet1.Cells.Add(2, 2, "名次", xf3);
            sheet1.Cells.Add(2, 3, "姓名", xf3);
            sheet1.Cells.Add(2, 4, "身份证号", xf3);
            sheet1.Cells.Add(2, 5, "报名单位", xf3);
            sheet1.Cells.Add(2, 6, "报名职务", xf3);
            sheet1.Cells.Add(2, 7, "笔试分数", xf3);
            sheet1.Cells.Add(2, 8, "面试分数", xf3);
            sheet1.Cells.Add(2, 9, "总分数", xf3);

            ColumnInfo colInfoxueli1 = new ColumnInfo(xls, sheet1);
            colInfoxueli1.Width = 15 * 256;
            colInfoxueli1.ColumnIndexStart = 3;
            colInfoxueli1.ColumnIndexEnd = 3;
            sheet1.AddColumnInfo(colInfoxueli1);

            ColumnInfo colInfoxueli2 = new ColumnInfo(xls, sheet1);
            colInfoxueli2.Width = 25 * 256;
            colInfoxueli2.ColumnIndexStart = 4;
            colInfoxueli2.ColumnIndexEnd = 4;
            sheet1.AddColumnInfo(colInfoxueli2);

            ColumnInfo colInfoxueli3 = new ColumnInfo(xls, sheet1);
            colInfoxueli3.Width = 20 * 256;
            colInfoxueli3.ColumnIndexStart = 5;
            colInfoxueli3.ColumnIndexEnd = 5;
            sheet1.AddColumnInfo(colInfoxueli3);

            for (int i = 0; i < selectedempmodel.Count; i++)
            {
                sheet1.Cells.Add(i + 3, 1, i + 1, xf);
                sheet1.Cells.Add(i + 3, 2, selectedempmodel[i].MingCi.ToString(), xf);
                if (selectedempmodel[i].XingMing != null) sheet1.Cells.Add(i + 3, 3, selectedempmodel[i].XingMing.ToString(), xf);
                if (selectedempmodel[i].SFZH != null) sheet1.Cells.Add(i + 3, 4, selectedempmodel[i].SFZH.ToString(), xf);
                if (selectedempmodel[i].YRBM != null) sheet1.Cells.Add(i + 3, 5, selectedempmodel[i].YRBM.ToString(), xf);
                if (selectedempmodel[i].BBZW != null) sheet1.Cells.Add(i + 3, 6, selectedempmodel[i].BBZW.ToString(), xf);
                if (selectedempmodel[i].ZongFen != null) sheet1.Cells.Add(i + 3, 7, selectedempmodel[i].ZongFen.ToString(), xf);
                if (selectedempmodel[i].MianShiScore != null) sheet1.Cells.Add(i + 3, 8, selectedempmodel[i].MianShiScore.ToString(), xf);
                if (selectedempmodel[i].TotalScore != null) sheet1.Cells.Add(i + 3, 9, selectedempmodel[i].TotalScore.ToString(), xf);
            }
            xls.Save(ss);
            ss.Close();
            ss.Dispose();
            SilverlightMessageBoxLibrary.Message.InfoMessage("导出成功！");
        }

        private void ChaData_Click(object sender, RoutedEventArgs e)
        {
            Load();
        }

        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            foreach (ZhaoLu_DaoRuKaoShengModel p in DGRole.ItemsSource)
            {
                p.IsSelected = (bool)cb.IsChecked;
                if (p.IsSelected)
                {
                    if (selectedempmodel.Where(i => i.ID == p.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(p);
                    }
                }
                else
                {
                    if (selectedempmodel.Where(i => i.ID == p.ID).FirstOrDefault() != null)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(i => i.ID == p.ID).FirstOrDefault());
                    }
                }
            }
        }
    }
}
